package com.zzptc.mzb.myapplication.Frament;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zzptc.mzb.myapplication.Bean.User;
import com.zzptc.mzb.myapplication.LoginMain2Activity;
import com.zzptc.mzb.myapplication.R;

import org.litepal.LitePal;


public class ThirdBlankFragment extends Fragment {
    private EditText oldb,newp;
    private Button chang;
    public ThirdBlankFragment() {

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //实例化
      View view= inflater.inflate(R.layout.fragment_third_blank, container, false);
      initView(view);
      setListener();
      return  view;
    }

    private void setListener() {
        //点击事件
        //修改按钮
        chang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加原来密码并修改密码，并转值给psd_old ，新密码给psd_new
                String psd_old=oldb.getText().toString().trim();
                String psd_new=newp.getText().toString().trim();
                SharedPreferences sp=getActivity().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
                int uid=sp.getInt("userid",-1);
                String password= sp.getString("password","");
                if (psd_old.equals(password)){//判断修改密码与原密码进行对比
                    User user=LitePal.find(User.class,uid);
                    user.setPassword(psd_new);
                    user.save();
                    showToast("修改密码成功，请重新登录");
                    Intent intent=new Intent(getContext(), LoginMain2Activity.class);
                    startActivity(intent);
                    getActivity().fileList();
                }else {
                    showToast("原密码输入错误");
                }
            }
        });
    }

private  void  showToast(String str){
    Toast.makeText(getContext(),str,Toast.LENGTH_SHORT).show();
}
    private void initView(View view) {
        oldb=view.findViewById(R.id.oldp);
        newp=view.findViewById(R.id.newp);
        chang=view.findViewById(R.id.chang);

    }
}


